﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 数据访问类:ItemPurchase
    /// </summary>
    public partial class ItemPurchase
    {
        public ItemPurchase()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal FItemPurchaseID, string FEntityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_ItemPurchase");
            strSql.Append(" where FItemPurchaseID=@FItemPurchaseID and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemPurchaseID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = FItemPurchaseID;
            parameters[1].Value = FEntityNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ElingOA6_Admin_DSJCJ.Model.ItemPurchase model, List<Model.ItemPurchaseEntry> ItemPurchaseEntryList, List<Model.Item> ItemList)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_ItemPurchase(");
            strSql.Append("FInsDate,FEntityNo,FInsEmplNo,FNote,FInsIP)");
            strSql.Append(" values (");
            strSql.Append("@FInsDate,@FEntityNo,@FInsEmplNo,@FNote,@FInsIP)");
            strSql.Append(";select @returnBId=IDENT_CURRENT('dbo.t_ItemPurchase')");
            SqlParameter[] parameters = {
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FNote", SqlDbType.NVarChar,200),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
                    new SqlParameter("@returnBId", SqlDbType.Decimal,13)};
            parameters[0].Value = model.FInsDate;
            parameters[1].Value = model.FEntityNo;
            parameters[2].Value = model.FInsEmplNo;
            parameters[3].Value = model.FNote;
            parameters[4].Value = model.FInsIP;
            parameters[5].Direction = ParameterDirection.Output;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            foreach (Model.ItemPurchaseEntry ItemPurchaseEntry in ItemPurchaseEntryList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into dbo.t_ItemPurchaseEntry(");
                strSql.Append("FEntityNo,FItemNo,FQty,FPrice,FAmount,FItemPurchaseID,FPurchaseDate)");
                strSql.Append(" values (");
                strSql.Append("@FEntityNo,@FItemNo,@FQty,@FPrice,@FAmount,@FItemPurchaseID,@FPurchaseDate)");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FItemNo", SqlDbType.NVarChar,25),
					new SqlParameter("@FQty", SqlDbType.Decimal,13),
					new SqlParameter("@FPrice", SqlDbType.Decimal,13),
					new SqlParameter("@FAmount", SqlDbType.Decimal,13),
					new SqlParameter("@FItemPurchaseID", SqlDbType.Decimal,13),
					new SqlParameter("@FPurchaseDate", SqlDbType.DateTime)};
                parameters2[0].Value = ItemPurchaseEntry.FEntityNo;
                parameters2[1].Value = ItemPurchaseEntry.FItemNo;
                parameters2[2].Value = ItemPurchaseEntry.FQty;
                parameters2[3].Value = ItemPurchaseEntry.FPrice;
                parameters2[4].Value = ItemPurchaseEntry.FAmount;
                parameters2[5].Direction = ParameterDirection.InputOutput;
                parameters2[6].Value = ItemPurchaseEntry.FPurchaseDate;

                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }


            foreach (Model.Item Item in ItemList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("update dbo.t_Item set ");
                strSql.Append("FPrice=@FPrice,");
                strSql.Append("FQty=@FQty,");
                strSql.Append("FAmount=@FAmount");
                strSql.Append(" where FItemNo=@FItemNo and FEntityNo=@FEntityNo");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FPrice", SqlDbType.Decimal,13),
					new SqlParameter("@FQty", SqlDbType.Decimal,13),
					new SqlParameter("@FAmount", SqlDbType.Decimal,13),
					new SqlParameter("@FItemNo", SqlDbType.NVarChar,25),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
                parameters2[0].Value = Item.FPrice;
                parameters2[1].Value = Item.FQty;
                parameters2[2].Value = Item.FAmount;
                parameters2[3].Value = Item.FItemNo;
                parameters2[4].Value = Item.FEntityNo;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ElingOA6_Admin_DSJCJ.Model.ItemPurchase model, List<Model.ItemPurchaseEntry> ItemPurchaseEntryList, List<Model.Item> ItemList)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_ItemPurchase set ");
            strSql.Append("FNote=@FNote,");
            strSql.Append("FUpdDate=@FUpdDate,");
            strSql.Append("FUpdIP=@FUpdIP,");
            strSql.Append("FUpdEmplNo=@FUpdEmplNo");
            strSql.Append(" where FItemPurchaseID=@FItemPurchaseID and FEntityNo=@FEntityNo");
            SqlParameter[] parameters = {
					new SqlParameter("@FNote", SqlDbType.NVarChar,200),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FItemPurchaseID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FUpdEmplNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FNote;
            parameters[1].Value = model.FUpdDate;
            parameters[2].Value = model.FUpdIP;
            parameters[3].Value = model.FItemPurchaseID;
            parameters[4].Value = model.FEntityNo;
            parameters[5].Value = model.FUpdEmplNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_ItemPurchaseEntry ");
            strSql.Append(" where FItemPurchaseID=@FItemPurchaseID and FEntityNo=@FEntityNo");
            SqlParameter[] parameters3 = {
					new SqlParameter("@FItemPurchaseID", SqlDbType.Decimal,13),
                    new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)
};

            parameters3[0].Value = model.FItemPurchaseID;
            parameters3[1].Value = model.FEntityNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters3;
            cmdList.Add(cmds);


            foreach (Model.ItemPurchaseEntry ItemPurchaseEntry in ItemPurchaseEntryList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into dbo.t_ItemPurchaseEntry(");
                strSql.Append("FEntityNo,FItemNo,FQty,FPrice,FAmount,FItemPurchaseID,FPurchaseDate)");
                strSql.Append(" values (");
                strSql.Append("@FEntityNo,@FItemNo,@FQty,@FPrice,@FAmount,@FItemPurchaseID,@FPurchaseDate)");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FItemNo", SqlDbType.NVarChar,25),
					new SqlParameter("@FQty", SqlDbType.Decimal,13),
					new SqlParameter("@FPrice", SqlDbType.Decimal,13),
					new SqlParameter("@FAmount", SqlDbType.Decimal,13),
					new SqlParameter("@FItemPurchaseID", SqlDbType.Decimal,13),
					new SqlParameter("@FPurchaseDate", SqlDbType.DateTime)};
                parameters2[0].Value = ItemPurchaseEntry.FEntityNo;
                parameters2[1].Value = ItemPurchaseEntry.FItemNo;
                parameters2[2].Value = ItemPurchaseEntry.FQty;
                parameters2[3].Value = ItemPurchaseEntry.FPrice;
                parameters2[4].Value = ItemPurchaseEntry.FAmount;
                parameters2[5].Value = model.FItemPurchaseID;
                parameters2[6].Value = ItemPurchaseEntry.FPurchaseDate;

                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }


            foreach (Model.Item Item in ItemList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("update dbo.t_Item set ");
                strSql.Append("FPrice=@FPrice,");
                strSql.Append("FQty=@FQty,");
                strSql.Append("FAmount=@FAmount");
                strSql.Append(" where FItemNo=@FItemNo and FEntityNo=@FEntityNo");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FPrice", SqlDbType.Decimal,13),
					new SqlParameter("@FQty", SqlDbType.Decimal,13),
					new SqlParameter("@FAmount", SqlDbType.Decimal,13),
					new SqlParameter("@FItemNo", SqlDbType.NVarChar,25),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
                parameters2[0].Value = Item.FPrice;
                parameters2[1].Value = Item.FQty;
                parameters2[2].Value = Item.FAmount;
                parameters2[3].Value = Item.FItemNo;
                parameters2[4].Value = Item.FEntityNo;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(decimal FItemPurchaseID, List<Model.Item> ItemList)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();

            strSql.Append("delete from dbo.t_ItemPurchaseEntry ");
            strSql.Append(" where FItemPurchaseID=@FItemPurchaseID");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemPurchaseID", SqlDbType.Decimal)
			};
            parameters[0].Value = FItemPurchaseID;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_ItemPurchase ");
            strSql.Append(" where FItemPurchaseID=@FItemPurchaseID");
            SqlParameter[] parameters2 = {
					new SqlParameter("@FItemPurchaseID", SqlDbType.Decimal)
			};
            parameters2[0].Value = FItemPurchaseID;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters2;
            cmdList.Add(cmds);

            foreach (Model.Item Item in ItemList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("update dbo.t_Item set ");
                strSql.Append("FPrice=@FPrice,");
                strSql.Append("FQty=@FQty,");
                strSql.Append("FAmount=@FAmount");
                strSql.Append(" where FItemNo=@FItemNo and FEntityNo=@FEntityNo");
                SqlParameter[] parameters3 = {
					new SqlParameter("@FPrice", SqlDbType.Decimal,13),
					new SqlParameter("@FQty", SqlDbType.Decimal,13),
					new SqlParameter("@FAmount", SqlDbType.Decimal,13),
					new SqlParameter("@FItemNo", SqlDbType.NVarChar,25),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
                parameters3[0].Value = Item.FPrice;
                parameters3[1].Value = Item.FQty;
                parameters3[2].Value = Item.FAmount;
                parameters3[3].Value = Item.FItemNo;
                parameters3[4].Value = Item.FEntityNo;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters3;
                cmdList.Add(cmds);
            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);

        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public void DeleteList(string FItemPurchaseIDlist, List<Model.Item> ItemList)
        {

            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            StringBuilder strSql = new StringBuilder();

            strSql.Append("delete from dbo.t_ItemPurchaseEntry ");
            strSql.Append(" where FItemPurchaseID in (" + FItemPurchaseIDlist + ")  ");

            cmds.CommandText = strSql.ToString();
            SqlParameter[] abc = { new SqlParameter() };
            cmds.Parameters = abc;
            cmdList.Add(cmds);

            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_ItemPurchase ");
            strSql.Append("where FItemPurchaseID in (" + FItemPurchaseIDlist + ")  ");

            cmds.CommandText = strSql.ToString();
            SqlParameter[] abc2 = { new SqlParameter() };
            cmds.Parameters = abc2;
            cmdList.Add(cmds);

            foreach (Model.Item Item in ItemList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("update dbo.t_Item set ");
                strSql.Append("FPrice=@FPrice,");
                strSql.Append("FQty=@FQty,");
                strSql.Append("FAmount=@FAmount");
                strSql.Append(" where FItemNo=@FItemNo and FEntityNo=@FEntityNo");
                SqlParameter[] parameters3 = {
					new SqlParameter("@FPrice", SqlDbType.Decimal,13),
					new SqlParameter("@FQty", SqlDbType.Decimal,13),
					new SqlParameter("@FAmount", SqlDbType.Decimal,13),
					new SqlParameter("@FItemNo", SqlDbType.NVarChar,25),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
                parameters3[0].Value = Item.FPrice;
                parameters3[1].Value = Item.FQty;
                parameters3[2].Value = Item.FAmount;
                parameters3[3].Value = Item.FItemNo;
                parameters3[4].Value = Item.FEntityNo;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters3;
                cmdList.Add(cmds);
            }

            DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);

            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("delete from dbo.t_ItemPurchase ");
            //strSql.Append(" where FItemPurchaseID in (" + FItemPurchaseIDlist + ")  ");
            //int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            //if (rows > 0)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.ItemPurchase GetModel(decimal FItemPurchaseID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FItemPurchaseID,FInsDate,FEntityNo,FInsEmplNo,FNote,FInsIP,FUpdEmplNo,FUpdDate,FUpdIP from dbo.t_ItemPurchase ");
            strSql.Append(" where FItemPurchaseID=@FItemPurchaseID");
            SqlParameter[] parameters = {
					new SqlParameter("@FItemPurchaseID", SqlDbType.Decimal)
			};
            parameters[0].Value = FItemPurchaseID;

            ElingOA6_Admin_DSJCJ.Model.ItemPurchase model = new ElingOA6_Admin_DSJCJ.Model.ItemPurchase();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.ItemPurchase DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.ItemPurchase model = new ElingOA6_Admin_DSJCJ.Model.ItemPurchase();
            if (row != null)
            {
                if (row["FItemPurchaseID"] != null && row["FItemPurchaseID"].ToString() != "")
                {
                    model.FItemPurchaseID = decimal.Parse(row["FItemPurchaseID"].ToString());
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FInsEmplNo"] != null)
                {
                    model.FInsEmplNo = row["FInsEmplNo"].ToString();
                }
                if (row["FNote"] != null)
                {
                    model.FNote = row["FNote"].ToString();
                }
                if (row["FInsIP"] != null)
                {
                    model.FInsIP = row["FInsIP"].ToString();
                }
                if (row["FUpdEmplNo"] != null)
                {
                    model.FUpdEmplNo = row["FUpdEmplNo"].ToString();
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FUpdIP"] != null)
                {
                    model.FUpdIP = row["FUpdIP"].ToString();
                }
                if (row.Table.Columns.Contains("Counts"))
                {
                    if (row["Counts"] != null)
                    {
                        model.Counts = row["Counts"].ToString();
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FItemPurchaseID,FInsDate,FEntityNo,FInsEmplNo,FNote,FInsIP,FUpdEmplNo,FUpdDate,FUpdIP ");
            strSql.Append(" FROM dbo.t_ItemPurchase ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FItemPurchaseID,FInsDate,FEntityNo,FInsEmplNo,FNote,FInsIP,FUpdEmplNo,FUpdDate,FUpdIP ");
            strSql.Append(" FROM dbo.t_ItemPurchase ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM dbo.t_ItemPurchase ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FItemPurchaseID desc");
            }
            strSql.Append(")AS Row, T.*  from dbo.t_ItemPurchase T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_ItemPurchase";
            parameters[1].Value = "FItemPurchaseID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

